﻿using FangDuShi_Admin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FangDuShi_Admin.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return Redirect("~/Home/Login"); ;
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult Chat()
        {
            return View();
        }

        public ActionResult UserLogin()
        {
            bool isSuccess = true;
            string errorMessage = "";
            string loginID = Fetch.Post("username").Trim();
            if (loginID == "")
            {
                errorMessage = "用户名不能为空！";
                isSuccess = false;
            }

            //if (isSuccess)
            //{
            //    string verifyCode = Fetch.Post("txtVerifyCode").Trim();
            //    if (verifyCode == "")
            //    {
            //        errorMessage = "验证码不能为空！";
            //        isSuccess = false;
            //    }
            //    else
            //    {
            //        if (Request.Cookies["ValidateCode"] == null)
            //        {
            //            errorMessage = "验证码错误请重新填写！";
            //            isSuccess = false;
            //        }
            //        else
            //        {
            //            if (verifyCode != Request.Cookies["ValidateCode"].Value.Trim().ToLower())
            //            {
            //                errorMessage = "验证码错误，请重新填写！";
            //                isSuccess = false;
            //            }
            //        }
            //    }
            //}

            if (isSuccess)
            {
                string password = Fetch.Post("password").Trim();
                int result = SysLogin(loginID, password);
                if (result == 1)
                {
                    errorMessage = "用户名或密码错误请重新填写！";
                    isSuccess = false;
                }
                else if (result == 2)
                {
                    errorMessage = "你的帐号已经被禁止登陆！";
                    isSuccess = false;
                }
                else if (result == 3)
                {

                    errorMessage = "你的用户名已过期，不能登陆！";
                    isSuccess = false;
                }
                else if (result == 4)
                {
                    errorMessage = "ip受限制,请联系管理员!";
                }
                else if (result == 200)
                {
                    errorMessage = "";
                }
            }

            ViewData["username"] = loginID;
            if (errorMessage != "")
            {
                ViewData["jsAlert"] = errorMessage;
                return View("Login");
            }
            else
            {


               return Redirect(Url.Action("Index", "Admin"));
               
                   
               
            }

        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private int SysLogin(string loginID, string password)
        {
            BLL.Login adminLoginBLL = new BLL.Login();
            Model.PrvUser prvUserModel = adminLoginBLL.GetModelByNameAndPassword(loginID, password);
            if (prvUserModel == null) //用户名或密码错误
            {
                return 1;
            }
            else
            {
                if (prvUserModel.FProhibitLogin == "1")
                {
                    return 2;
                }
                else if (prvUserModel.FForeverAvailable != "1")//如果账号不是永久有效，就判断该账号是否已过期
                {
                    DateTime timenow = DateTime.Now.AddDays(-1);
                    if (timenow > prvUserModel.FAvailableDate)//已过期
                    {
                        return 3;
                    }
                }
             
                OperateSession.add("LoginID", prvUserModel.FLogID);//把登陆ID保存到Session
                OperateSession.add("UserName", prvUserModel.FUserName);//登陆用户名称
           

                prvUserModel.FLastLoginIPAddr = GenerateUtil.GetClientIPv4Address();
                adminLoginBLL.UpdateUserLoginMsg(prvUserModel.FLogID, prvUserModel.FLastLoginIPAddr);
                return 200;
            }

        }
    }
}